<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="p" uri="/lion-platform" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
	<link rel="stylesheet" href="${webcontext}/res/style/S.css" />
	<script type="text/javascript" src="${webcontext }/res/js/jquery.min.js"></script>
	<script type="text/javascript" src="${webcontext }/res/js/S.js"></script>
	<script type="text/javascript">
	
		var A = {};
	
		$(function(){
			
			var levels = {
					'1':'<span class="badge danger">系统缺陷</span>',
					'2':'<span class="badge warn">模块缺陷</span>',
					'3':'<span class="badge inverse">功能缺陷</span>',
					'4':'<span class="badge primary">常识缺陷</span>',
					'5':'<span class="badge info">建议性</span>',
			};
			
			var solveTypes = {
					'1':'<span class="badge">修改程序</span>',
					'2':'<span class="badge">修改数据库</span>',
					'3':'<span class="badge">调整运行环境</span>',
					'4':'<span class="badge">调整操作流程</span>',
					'5':'<span class="badge">有意为之</span>',
					'6':'<span class="badge">无法重现</span>',
					'7':'<span class="badge">误报</span>',
					'8':'<span class="badge">特例</span>',
					'9':'<span class="badge">转为新需求 </span>',
					'10':'<span class="badge">不处理</span>',
					'11':'<span class="badge">其他处理方式</span>'
			};
			
			var tbl =  new S.Table({
				el:'#tbl001',
				pager:true,
				pageSize:30,
				multiselect:false,
				renders:{
					'level':function(level){
						return  levels[level] || '';
					},
					'bugStatus':function(status){
						if(status == 2){return '<span class="badge primary">已解决</span>';}
						if(status == 3){return '<span class="badge success">已关闭</span>';}
						return '<span class="badge info">打　开</span>';
					},
					'solveType':function(type){
						return solveTypes[type] || '';
					},
				},
				dbclick:function(data){
					A.showBug(data.id);
				},
				menus:{
					'查看':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data==null){return;}
						A.showBug(data.id);
					},
					'加入TODO':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data==null){return;}
						addTODO([data.id]);
						
					},
					'删除':function(tr){
						var idx = $(tr).index();
						var data = tbl.data[idx];
						if(data==null){return;}
						deleteBug([data]);
					}
				}
			});
			
			
			var select = new S.Select({
				el:'#projects_select',
				ds:'${webcontext}/${pathvar.admin}/br/project/projects',
				render:function(p){
					return p.name;
				},
				change:function(p){
					$('#modelId').val('');
					tbl.load('${webcontext}/${pathvar.admin}/br/bugs/list?projectId='+p.id);
					modelSelect.load('${webcontext}/${pathvar.admin}/br/project/modules/list?projectId='+p.id);
				}
			});
			
			
			$('#btn_query').click(function(){
				tbl.reload();
			});
			
			var modelSelect = new S.TreeSelect({
				el:'#project_model',
				render:function(m){
					return '<a class="icons books_stack"></a>'+m.name;
				},
				click:function(m){
					if(m){
						$('#modelId').val(m.id);
					}else{
						$('#modelId').val('');
					}
				}
			});
			
			select.select.defer(select,200,0);
			
			
			$('#btn_bug_add').click(function(){
				
				var ps = select.getSelectedItem();
				if(!ps){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				var dialog = new S.Dialog({
					title:'报告缺陷',
					width:900,
					height:'80%',
					modal:true,
					url:'${webcontext}/${pathvar.admin}/br/bugs/edit?projectId='+ps.id,
					buttons:{
						'保存':function(){
							
							if(!this.form.validate()){return;}
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/bugs/save',
								data:S.fixConfig({projectId:ps.id},this.form.param()),
								success:function(){
									new S.Info({text:'保存成功',type:'success'}).show();
									dialog.remove();
									tbl.reload();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dialog.show();
			});
			
			$('#btn_bug_edit').click(function(){
				var items = tbl.getCheckedRow();
				if(items.length == 0){new S.Info({text:'请选择Bug',type:'warn'}).show();return;}
				A.showBug(items[0].id);
			});
			
			
			function deleteBug(items){
				S.confirm('确认','确认要删除选中的Bug?',function(){
					var ids = [];
					S.each(items,function(i,item){
						ids.push(item.id);
					});
					
					S.request({
						url:'${webcontext}/${pathvar.admin}/br/bugs/delete',
						data:{ids:ids.join(',')},
						success:function(){
							new S.Info({text:'删除成功',type:'success'}).show();
							tbl.reload();
						}
					});
				},'warn');
			}
			
			$('#btn_bug_delete').click(function(){
				
				var items = tbl.getCheckedRow();
				if(items.length == 0){new S.Info({text:'请选择Bug',type:'warn'}).show();return;}
				deleteBug(items);
			});
			
			
			new S.AutoComplete({
				el:'#b_owner',
				url:'${webcontext}/${pathvar.admin}/user/search',
				render:function(data){
					return '<b>'+data.realName+'</b> <span>'+data.orgName+'</span>';
				},
				click:function(d){
					$('#pbugowner').val(d.id);
					$('#b_owner').val(d.realName);
				}
			});
			
			A.showBug= function(id){
				var dialog = new S.Dialog({
					title:'编辑缺陷',
					width:900,
					height:'80%',
					modal:true,
					url:'${webcontext}/${pathvar.admin}/br/bugs/edit?id='+id,
					buttons:{
						'保存':function(){
							
							if(!this.form.validate()){return;}
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/bugs/update',
								data:this.form.param(),
								success:function(){
									new S.Info({text:'修改成功',type:'success'}).show();
									dialog.remove();
									tbl.reload();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
					
				});
				dialog.show();
			};
			
			
			$('#btn_export_bugs').click(function(){
				
				var ps = select.getSelectedItem();
				if(!ps){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				var param = S.formData(tbl.$form);
				param.projectId=ps.id;
				
				S.download('${webcontext}/${pathvar.admin}/br/bugs/export',param);
			});
			
			$('#btn_bug_add_todo').click(function(){
				var items = tbl.getCheckedRow();
				if(items.length == 0){new S.Info({text:'请选择Bug',type:'warn'}).show();return;}
				
				var ids = [];
				S.each(items,function(i,item){
					ids.push(item.id);
				});
				addTODO(ids);
			});
			
			function addTODO(ids){
				S.request({
					url:'${webcontext}/${pathvar.admin}/br/bugs/addtodo',
					data:{ids:ids.join(',')},
					success:function(){
						new S.Info({text:'加入成功',type:'success'}).show();
					}
				});
			}
		});	
	</script>
</head>
<body>
	<div class="filled-box">
		<div class="toolbar">
			<span>　项目<input id="projects_select" type="text"></span>
			<span class="btn" id="btn_bug_add"><i class="icons plus"></i> 增加</span>
			<span class="btn" id="btn_bug_edit"><i class="icons pencil"></i> 编辑</span>
			<p:priv code="BR.B.DEL">
			<span class="btn" id="btn_bug_delete"><i class="icons cross"></i> 删除</span>
			</p:priv>
			<span class="btn" id="btn_export_bugs"><i class="icons saved_exports"></i> 导出</span>
			<span class="btn" id="btn_bug_add_todo"><i class="icons show_source_documents"></i> 加入TODO</span>
		</div>
		<div class="content">
			<div>
				<form name="frm001">
					<input type="hidden" name="modelId" id="modelId" /> 
					<input type="hidden" name="owner" id="pbugowner">
					<table width="100%">
						<tr>
							<td style="text-align: left;">
								<span>BugID<input type="text" name="bugId"></span>
								<span>模块 <input type="text" id="project_model"> </span>  
								<span>指派给<input type="text" id="b_owner"  /></span>
								<span>状态<select name="bugStatus">
									<option value="">全部</option>
									<option value="1">打开</option>
									<option value="2">已解决</option>
									<option value="3">已关闭</option>
								</select></span> 
								 <span>等级<select name="level">
									<option value="">全部</option>
									<option value="1">系统缺陷</option>
									<option value="2">模块缺陷</option>
									<option value="3">功能缺陷</option>
									<option value="4">常识缺陷</option>
									<option value="5">建议性</option>
								</select></span>
								 <span class="btn"  id="btn_query"><i class="icons magnifier"></i>查找</span>								
							</td>							
						</tr>
					</table>					
				</form>
			</div>
			<table id="tbl001" form="frm001" height="600" ds="${webcontext }/${pathvar.admin}/blank">
				<tr>
					<td width="50" seq></td>
					<td width="50" checkall></td>
					<td width="50" >ID</td>
					<td width="80" col="level" sort="level">等级</td>
					<td resize="true">标题</td>
					<td width="100" col="bugStatus" sort="bugStatus" resize="true">状态</td>
					<td width="100" sort="owner" resize="true">指派给</td>
					<td width="100" sort="creator" resize="true">创建者</td>
					<td width="100" sort="createTime" resize="true">创建时间</td>
					<td width="100" sort="solveAccId" resize="true">解决者</td>
					<td width="100" sort="solveType" col="solveType" resize="true">解决方案</td>
					<td width="100" sort="closeAccId" resize="true">关闭者</td>
				</tr>
				<tr>
					<td>@{seq}</td>
					<td><input type="checkbox" name="id" value="@{id }" /></td>
					<td>#@{id}</td>
					<td>@{level}</td>
					<td>@{title}</td>
					<td>@{bugStatus}</td>
					<td>@{ownerName}</td>
					<td>@{creatorName}</td>
					<td>@{createTime}</td>
					<td>@{solveAccName}</td>
					<td>@{solveType}</td>
					<td>@{closeAccName}</td>
				</tr>
			</table>
		</div>
	</div>
</body>
</html>